<?php

namespace App\Http\Controllers\Activity\Merchant;

use App\Http\Controllers\BaseController;
use App\Models\Activity\ShopOrderActivityFeedbackModel;
use App\Models\Shop\ShopModel;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class ShopOrderActivityFeedbackController extends BaseController
{
    /**
     * 反馈记录详情
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function detail(Request $request): JsonResponse
    {
        $activityId = $request->input('activityId', 0);
        $shopId     = $request->header('shopId');

        $this->validate($request, [
            'activityId' => 'required|integer',
//            'shopId'     => 'required|integer',
        ]);

        $feedbackInfo = ShopOrderActivityFeedbackModel::query()
                                                      ->where('activityId', $activityId)
                                                      ->where('shopId', $shopId)
                                                      ->first();

        $result = empty($feedbackInfo) ? [] : $feedbackInfo->toArray();

        return $this->success($result);
    }

    public function save(Request $request)
    {
        $activityId     = $request->input('activityId', 0);
        $shopId         = $request->header('shopId');
        $feedbackImages = $request->input('feedbackImages', []);
        $feedbackVideos = $request->input('feedbackVideos', []);
        $feedback       = $request->input('feedback', '');
        $shopContact    = $request->input('shopContact', '');

        $this->validate($request, [
            'activityId'     => 'required|integer',
//            'shopId'         => 'required|integer',
//            'feedback'       => 'required',
            'shopContact'    => 'required',
            'feedbackImages' => 'required_without:feedbackVideos|array',
            'feedbackVideos' => 'required_without:feedbackImages|array'
        ]);

        $shopInfo = ShopModel::getDataById($shopId);

        ShopOrderActivityFeedbackModel::query()
                                      ->updateOrCreate([
                                          'shopId'     => $shopId,
                                          'activityId' => $activityId
                                      ], [
                                          'feedbackImages' => $feedbackImages,
                                          'feedbackVideos' => $feedbackVideos,
                                          'shopName'       => $shopInfo->name,
                                          'feedback'       => $feedback,
                                          'shopContact'    => $shopContact

                                      ]);

        return $this->success();
    }
}
